package com.crm.service.impl;

import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crm.dao.BizInfosBelongMapper;
import com.crm.dao.ConsultInfoManageMapper;
import com.crm.model.BizInfosBelong;
import com.crm.model.ConsultInfoManage;
import com.crm.model.ConsultInfoManagePay;
import com.crm.model.entity.CountWorkBench;
import com.crm.model.entity.KeyValue;
import com.crm.service.ConsultInfoManageService;
import com.crm.util.UUIDUtil;
import com.crm.util.ValidateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class ConsultInfoManageServiceImpl extends BaseServiceImpl<ConsultInfoManage> implements ConsultInfoManageService{
	@Autowired
	private ConsultInfoManageMapper consultInfoManageMapper;
	@Autowired
	private BizInfosBelongMapper infoBelongMapper;

	
	@Override
	public int insert(ConsultInfoManage record) {
		
		String uuid = UUIDUtil.getUUID();
		record.setInfoManageId(uuid);
		
		return super.insert(record);
	}
	
	@Override
	public List<ConsultInfoManage> selectAll(Page<ConsultInfoManage> page,ConsultInfoManage t) {
		// TODO Auto-generated method stub
		if(t.getStatus() != null){
			if(t.getStatus() == 2){
				PageHelper.startPage(page.getPageNum(), page.getPageSize(), "recordnexttime");
			}else if(t.getStatus() == 3){
				PageHelper.startPage(page.getPageNum(), page.getPageSize(), "recordnexttime");
			}else if(t.getStatus() == 4){
				PageHelper.startPage(page.getPageNum(), page.getPageSize(), "serverDate desc");
			}else if(t.getStatus() == 6){
				PageHelper.startPage(page.getPageNum(), page.getPageSize(), "DingZDate desc");
			}else if(t.getStatus() == 7){
				PageHelper.startPage(page.getPageNum(), page.getPageSize(), "BaoMDate desc");
			}else{
				PageHelper.startPage(page.getPageNum(), page.getPageSize(), "create_date desc");
			}
			
			if(!"-1".equals(t.getMustPay())){
				PageHelper.startPage(page.getPageNum(), page.getPageSize(), "next_pay_time desc");
			}
		}else{
			PageHelper.startPage(page.getPageNum(), page.getPageSize(), "create_date desc");
		}
		
		List<ConsultInfoManage> list = consultInfoManageMapper.selectAllByWhere(t);
		
		return list;
	}

	@Override
	public List<KeyValue> loadSum(ConsultInfoManage record) {
		// TODO Auto-generated method stub
		return consultInfoManageMapper.loadSum(record);
	}

	@Override
	public List<ConsultInfoManage> selectFinanceGroupBalance(Page<ConsultInfoManage> page, ConsultInfoManage manage) {
		
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		
		List<ConsultInfoManage> list = consultInfoManageMapper.selectFinanceGroupBalance(manage);
		
		return list;
	}

	@Override
	public ConsultInfoManage getTotalIncome(ConsultInfoManage manage) {
		return consultInfoManageMapper.getTotalIncome(manage);
	}

	@Override
	public List<CountWorkBench> task(CountWorkBench record) {
		// TODO Auto-generated method stub
		return consultInfoManageMapper.task(record);
	}

	@Override
	public CountWorkBench localCount(CountWorkBench record) {
		// TODO Auto-generated method stub
		return consultInfoManageMapper.localCount(record);
	}

	@Override
	public List<ConsultInfoManage> selectFinanceSchoolBalance(
			Page<ConsultInfoManage> page, ConsultInfoManage manage) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		return consultInfoManageMapper.selectFinanceSchoolBalance(manage);
	}

	@Override
	public ConsultInfoManage getTopOne(ConsultInfoManage record) {
		// TODO Auto-generated method stub
		return consultInfoManageMapper.getTopOne(record);
	}
	
	@Override
	public List<ConsultInfoManage> selectFinanceGroupReceivableBalance(
			Page<ConsultInfoManage> page, ConsultInfoManage manage) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), "cim.create_date desc");
		return consultInfoManageMapper.selectFinanceGroupReceivableBalance(manage);
	}

	@Override
	public ConsultInfoManage getReceivableTotalIncome(ConsultInfoManage t) {
		// TODO Auto-generated method stub
		return consultInfoManageMapper.getReceivableTotalIncome(t);
	}

	@Override
	public List<ConsultInfoManagePay> getPayByInfoId(String infoManageId) {
		// TODO Auto-generated method stub
		return consultInfoManageMapper.getPayByInfoId(infoManageId);
	}

	@Override
	public ConsultInfoManage getSchoolTotalNum(ConsultInfoManage t) {
		// TODO Auto-generated method stub
		return consultInfoManageMapper.getSchoolTotalNum(t);
	}

	@Override
	public ConsultInfoManage loadPersonal(ConsultInfoManage t) {
		// TODO Auto-generated method stub
		return consultInfoManageMapper.loadPersonal(t);
	}

	@Override
	public List<ConsultInfoManage> selectView(Page<ConsultInfoManage> page, ConsultInfoManage t) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), "create_date desc");
		
		List<ConsultInfoManage> list = consultInfoManageMapper.selectView(t);
		
		return list;
	}

	@Override
	public ConsultInfoManage selectByPrimaryKey2(String infoManageId) {
		return consultInfoManageMapper.selectByPrimaryKey2(infoManageId);
	}

	@Override
	public ConsultInfoManage getDepartment2Id(ConsultInfoManage t) {
		
		String brandId= t.getBrandId();
		String departmentId1 = t.getDepartmentId1();
		String brandSchool = "^\\[[[:print:]]*\\{\\\"brand\\\"\\:\\\""+brandId+"\\\"\\,\\\"school\\\"\\:\\[[a-zA-Z0-9\\-\\,\\\"]*\\\""+departmentId1+"\\\"[a-zA-Z0-9\\-\\,\\\"]*\\]\\}[[:print:]]*\\]$";
		String projectId = t.getProjectId();
		
		BizInfosBelong bizInfoBelong = new BizInfosBelong();
		bizInfoBelong.setBrandSchool(brandSchool);
		bizInfoBelong.setProjects(projectId);
		
		List<BizInfosBelong>  list =  infoBelongMapper.selectAllByWhere(bizInfoBelong);
		if(list.size()>0){
			String depId = list.get(0).getDepartmentId();
			String depName = list.get(0).getSysDepartment().getFullName();
			t.setDepartmentId2(depId);
			t.setDepartmentName2(depName);
			return t;
		}
		t.setDepartmentId2(departmentId1);
		t.setDepartmentName2(t.getDepartmentName1());
		return t;
	}
	
	@Override
	public ConsultInfoManage getDepartmentId2FromRed(ConsultInfoManage t) {
		//品牌ID，分校ID，拼字符串
		String brandId= t.getBrandId();
		String departmentId1 = t.getDepartmentId1();
		String brandSchool = "^\\[[[:print:]]*\\{\\\"brand\\\"\\:\\\""+brandId+"\\\"\\,\\\"school\\\"\\:\\[[a-zA-Z0-9\\-\\,\\\"]*\\\""+departmentId1+"\\\"[a-zA-Z0-9\\-\\,\\\"]*\\]\\}[[:print:]]*\\]$";
		
		//项目ID
		String projectId = t.getProjectId();
		BizInfosBelong bizInfoBelong = new BizInfosBelong();
		bizInfoBelong.setBrandSchool(brandSchool);
		bizInfoBelong.setProjects(projectId);
		
		//当前梯队+1
		String echelonId = t.getEchelonId();
		if(!ValidateUtil.isEmpty(echelonId)&&ValidateUtil.isNumber(echelonId)){
			echelonId=String.valueOf(Integer.parseInt(echelonId)+1);
		}
		
		List<BizInfosBelong>  list =  infoBelongMapper.selectAllByRed(bizInfoBelong,echelonId);
		if(list.size()>0){
			String depId = list.get(0).getDepartmentId();
			String depName = list.get(0).getSysDepartment().getFullName();
			t.setDepartmentId2(depId);
			t.setDepartmentName2(depName);
			return t;
		}
		return null;
	}
	
	@Override
	public List<ConsultInfoManage> selectAllM(Page<ConsultInfoManage> page,Map<String, Object> map) {
		
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), "cim.create_date desc");
		
		List<ConsultInfoManage> list = consultInfoManageMapper.selectAllByWhereM(map);
		
		return list;
	}
	
	@Override
	public List<ConsultInfoManage> selectRedistributionInfo() {
		
		List<ConsultInfoManage> list = consultInfoManageMapper.selectRedistributionInfo();
		
		return list;
	}

	@Override
	public List<ConsultInfoManage> selectAllD(Page<ConsultInfoManage> page,Map<String, Object> map) {
		
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), "cim.create_date desc");
		
		List<ConsultInfoManage> list = consultInfoManageMapper.selectAllByWhereD(map);
		
		return list;
	}

	@Override
	public CountWorkBench localGroup(CountWorkBench record) {
		// TODO Auto-generated method stub
		return consultInfoManageMapper.localGroup(record);
	}

	@Override
	public CountWorkBench localSum(CountWorkBench record) {
		// TODO Auto-generated method stub
		return consultInfoManageMapper.localSum(record);
	}

	@Override
	public CountWorkBench localSumSort(CountWorkBench record) {
		// TODO Auto-generated method stub
		return consultInfoManageMapper.localSumSort(record);
	}

	@Override
	public CountWorkBench loadMustPaySum(ConsultInfoManage record) {
		// TODO Auto-generated method stub
		return consultInfoManageMapper.loadMustPaySum(record);
	}

}
